<?php
// php 位运算

class Test
{
    // 院校特色标签
    /**
     * 获取院校特色标签
     */
    static public function features()
    {
        return [
            '211' => 0, // 2^0=1
            '985' => 1, // 2^1=2
            '双一流' => 2, // 2^2=4
            '中央部委' => 3, // 2^3=8
            '强基计划' => 4, // 2^4=16
            '双高计划' => 5, // 2^5=32
            '双一流A' => 6, // 2^6=64
            '双一流B' => 7, // 2^7=128
            '一流学科建设' => 8, // 2^8=256
            '国重点' => 9, // 2^9=512
            '省重点' => 10, // 2^10=1024
            '省属' => 11, // 2^11=2048
        ];
    }

    // 返回特色标签的值（选项值累加）
    function foldSpecialTags(array $data)
    {
        // 自动剔除非数值内容
        return array_sum($data);
    }
    // 返回特色标签的数据列表（用于前端表单控件的数据源）
    public function getFeatureTagsList($valueField = 'value', $textField = 'text')
    {
        $data = [];
        foreach (self::features() as $text => $i) {
            $data[] = [$textField => $text, $valueField => $i];
        }
        return $text;
    }
    // 返回已选择的特色标签数据（展开已选择的选项）
    function getFeatureTags($val)
    {
        $data = [];
        if (!$val || !intval($val)) {
            return $data;
        }
        $val = intval($val);
        foreach (self::features() as $text => $i) {
            echo $val, ' & ', (0x1 << $i), ' == ', ($val & 0x1 << $i), PHP_EOL;
            if ($val & 0x1 << $i) {
                $data[strval($i)] = $text;
            }
        }
        return $data;
    }
}

class Test2
{
    protected $featureArr = ['211' => 0, '985' => 1, '双一流' => 2, '中央部委' => 3, '强基计划' => 4, '双高计划' => 5];
    protected $featureData = ['f211' => 0, 'f985' => 1, 'dual_class' => 2, 'is_central' => 3, 'is_strong_base' => 4, 'is_double_high' => 5];

    public function featureToInt($info)
    {
        $feature = 0;
        $info['f211'] == 1 && $feature = $feature | 0x1 << $this->featureData['f211'];
        $info['f985'] == 1 && $feature = $feature | 0x1 << $this->featureData['f985'];
        in_array($info['dual_class'], ['38000', '38001', '38002']) && $feature = $feature | 0x1 << $this->featureData['dual_class'];
        $info['is_central'] == 1 && $feature = $feature | 0x1 << $this->featureData['is_central'];
        $info['is_strong_base'] == 1 && $feature = $feature | 0x1 << $this->featureData['is_strong_base'];
        $info['is_double_high'] == 1 && $feature = $feature | 0x1 << $this->featureData['is_double_high'];

        return $feature;
    }
    public function featureToZH($feature)
    {
        if ($feature) {
            $item = [];
            foreach ($this->featureArr as $k => $v) {
                if ($feature & 0x1 << $v) {
                    $item[] = $k;
                }
            }

            return implode('/', $item);
        } else {
            return '';
        }
    }
}

$test = new Test();

$val = array_sum([1, 2, 32]);
echo 'val = ', $val, PHP_EOL;
$val = 599;
var_export($test->getFeatureTags($val));

$test2 = new Test2();
